package com.our123.cms.manager.assist.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.our123.cms.dao.assist.CmsCommentDao;
import com.our123.cms.dao.assist.CmsCommentReplyDao;
import com.our123.cms.entity.assist.CmsComment;
import com.our123.cms.entity.assist.CmsCommentExt;
import com.our123.cms.entity.assist.CmsCommentReply;
import com.our123.cms.entity.assist.CmsTrends;
import com.our123.cms.entity.main.CmsUser;
import com.our123.cms.entity.main.Content;
import com.our123.cms.manager.assist.CmsCommentExtMng;
import com.our123.cms.manager.assist.CmsCommentMng;
import com.our123.cms.manager.assist.CmsSensitivityMng;
import com.our123.cms.manager.assist.CmsTrendsMng;
import com.our123.cms.manager.main.CmsSiteMng;
import com.our123.cms.manager.main.CmsUserMng;
import com.our123.cms.manager.main.ContentCountMng;
import com.our123.cms.manager.main.ContentMng;
import com.our123.common.hibernate3.Updater;
import com.our123.common.page.Pagination;

@Service
@Transactional
public class CmsCommentMngImpl implements CmsCommentMng {
	@Transactional(readOnly = true)
	public Pagination getPage(Integer siteId, Integer channelId, Integer authorId, Integer contentId, Integer greaterThen, Boolean checked,
			boolean recommend, boolean desc, int pageNo, int pageSize) {
		Pagination page = dao.getPage(siteId, channelId, authorId, contentId, greaterThen, checked, recommend, desc, pageNo, pageSize, false);
		return page;
	}

	@Transactional(readOnly = true)
	public Pagination getPageForTag(Integer siteId, Integer channelId, Integer authorId, Integer contentId, Integer greaterThen, Boolean checked,
			boolean recommend, boolean desc, int pageNo, int pageSize) {
		Pagination page = dao.getPage(siteId, channelId, authorId, contentId, greaterThen, checked, recommend, desc, pageNo, pageSize, true);
		return page;
	}

	@Transactional(readOnly = true)
	public List<CmsComment> getListForTag(Integer siteId, Integer channelId, Integer authorId, Integer contentId, Integer greaterThen,
			Boolean checked, boolean recommend, boolean desc, int count) {
		return dao.getList(siteId, channelId, authorId, contentId, greaterThen, checked, recommend, desc, count, true);
	}

	@Transactional(readOnly = true)
	public CmsComment findById(Integer id) {
		CmsComment entity = dao.findById(id);
		return entity;
	}

	public CmsComment comment(String text, String ip, Integer contentId, Integer siteId, Integer userId, boolean checked, boolean recommend) {
		CmsComment comment = new CmsComment();
		Content c = contentMng.findById(contentId);
		comment.setContent(c);
		comment.setSite(cmsSiteMng.findById(siteId));
		CmsUser u = null;
		if (userId != null) {
			u = cmsUserMng.findById(userId);
			comment.setCommentUser(u);
		}
		comment.setChecked(checked);
		comment.setRecommend(recommend);
		comment.init();
		dao.save(comment);
		text = cmsSensitivityMng.replaceSensitivity(text);
		cmsCommentExtMng.save(ip, text, comment);
		contentCountMng.commentCount(contentId);
		//添加动态
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("contentId", c.getId());
		params.put("contentTitle", c.getTitle());
		params.put("commentContent", text);
		if (u != null) {
			params.put("commentUserId", u.getId());
			params.put("commentUserName", u.getUsername());
		}
		cmsTrendsMng.add(CmsTrends.CATEGORY_COMMENT, c.getUser().getId(), params);
		return comment;
	}

	public CmsComment responses(String text, Integer commentId, Integer userId) {
		CmsComment comment = dao.findById(commentId);
		if (comment != null) {
			comment.setReplayUser(cmsUserMng.findById(userId));
			comment.setReplayTime(new Date());
			CmsCommentExt ext = comment.getCommentExt();
			ext.setReply(text);
			return update(comment, ext);
		}
		return null;
	}

	public short reply(String text, String ip, Integer commentId, Integer userId) {
		CmsCommentReply reply = new CmsCommentReply();
		reply.setCreateTime(new Date());
		reply.setIp(ip);
		reply.setReplyText(text);
		CmsComment cmsComment = dao.findById(commentId);
		reply.setComment(cmsComment);
		CmsUser cmsUser = null;
		if (userId != null) {
			cmsUser = cmsUserMng.findById(userId);
			reply.setReplayUser(cmsUser);
		}
		//保存用户回复
		cmsCommentReplyDao.save(reply);
		//评论数+1
		cmsComment.setReplys((short) (cmsComment.getReplys() + 1));
		Updater<CmsComment> updater = new Updater<CmsComment>(cmsComment);
		cmsComment = dao.updateByUpdater(updater);
		//添加动态
		Map<String, Object> params = new HashMap<String, Object>();
		Content c = cmsComment.getContent();
		params.put("contentId", c.getId());
		params.put("contentTitle", c.getTitle());
		params.put("commentContent", cmsComment.getCommentExt().getText());
		params.put("replyContent", text);
		if (cmsUser != null) {
			params.put("replyUserId", cmsUser.getId());
			params.put("replyUserName", cmsUser.getUsername());
		}
		cmsTrendsMng.add(CmsTrends.CATEGORY_REPLY, cmsComment.getCommentUser().getId(), params);
		return cmsComment.getReplys();
	}

	public List<CmsCommentReply> replyList(Integer commentId) {
		if (commentId != null) {
			return cmsCommentReplyDao.getList(commentId);
		} else {
			return new ArrayList<CmsCommentReply>();
		}
	}

	public CmsComment update(CmsComment bean, CmsCommentExt ext) {
		Updater<CmsComment> updater = new Updater<CmsComment>(bean);
		bean = dao.updateByUpdater(updater);
		cmsCommentExtMng.update(ext);
		return bean;
	}

	public int deleteByContentId(Integer contentId) {
		cmsCommentExtMng.deleteByContentId(contentId);
		return dao.deleteByContentId(contentId);
	}

	public CmsComment deleteById(Integer id) {
		CmsComment bean = dao.deleteById(id);
		return bean;
	}

	public CmsComment[] deleteByIds(Integer[] ids) {
		CmsComment[] beans = new CmsComment[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	public void ups(Integer id) {
		CmsComment comment = findById(id);
		comment.setUps((short) (comment.getUps() + 1));
	}

	public void downs(Integer id) {
		CmsComment comment = findById(id);
		comment.setDowns((short) (comment.getDowns() + 1));
	}

	private CmsSensitivityMng cmsSensitivityMng;
	private CmsUserMng cmsUserMng;
	private CmsSiteMng cmsSiteMng;
	private ContentMng contentMng;
	private CmsTrendsMng cmsTrendsMng;
	private ContentCountMng contentCountMng;
	private CmsCommentExtMng cmsCommentExtMng;
	private CmsCommentReplyDao cmsCommentReplyDao;
	private CmsCommentDao dao;

	@Autowired
	public void setCmsSensitivityMng(CmsSensitivityMng cmsSensitivityMng) {
		this.cmsSensitivityMng = cmsSensitivityMng;
	}

	@Autowired
	public void setCmsUserMng(CmsUserMng cmsUserMng) {
		this.cmsUserMng = cmsUserMng;
	}

	@Autowired
	public void setCmsSiteMng(CmsSiteMng cmsSiteMng) {
		this.cmsSiteMng = cmsSiteMng;
	}

	@Autowired
	public void setContentMng(ContentMng contentMng) {
		this.contentMng = contentMng;
	}

	@Autowired
	public void setCmsTrendsMng(CmsTrendsMng cmsTrendsMng) {
		this.cmsTrendsMng = cmsTrendsMng;
	}

	@Autowired
	public void setCmsCommentExtMng(CmsCommentExtMng cmsCommentExtMng) {
		this.cmsCommentExtMng = cmsCommentExtMng;
	}

	@Autowired
	public void setContentCountMng(ContentCountMng contentCountMng) {
		this.contentCountMng = contentCountMng;
	}

	@Autowired
	public void setCmsCommentReplyDao(CmsCommentReplyDao cmsCommentReplyDao) {
		this.cmsCommentReplyDao = cmsCommentReplyDao;
	}

	@Autowired
	public void setDao(CmsCommentDao dao) {
		this.dao = dao;
	}

}